#include "GameLib.h"

void CVideo::VideoSet()
{
	set.ReadFunc();
	if (set.win_width == 0 || set.win_height == 0)
	{
		set.win_width = set.def_win_width;
		set.win_height = set.def_win_height;
	}
}

void CVideo::Init()
{
	VideoSet();
	const SDL_VideoInfo *VideoInfo = NULL;

	if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0)
		fprintf(stderr, "Attempt to get video info failed %s\n", SDL_GetError());
	atexit(SDL_Quit);

	VideoInfo = SDL_GetVideoInfo();
	if (VideoInfo == NULL)
	{
		fprintf(stderr, "Attempt to get video info failed %s\n", SDL_GetError());
	}
	atexit(SDL_Quit);

	unsigned int flags = SDL_OPENGL | SDL_DOUBLEBUF;
	if(set.isFullscreen) 
	flags |= SDL_FULLSCREEN;

	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);

	if(SDL_SetVideoMode(set.win_width, set.win_height, 32, flags) == NULL)//
	printf (SDL_GetError());

	glViewport(0, 0, set.win_width, set.win_height);//
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0, set.win_width / set.win_height, 0.1, 45.0);//
	glMatrixMode(GL_MODELVIEW);

	///glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
	//glEnable(GL_COLOR_MATERIAL);

	SDL_WM_SetCaption("PORPG","ex1");
	//SDL_WM_SetIcon(SDL_LoadBMP("icon.bmp"), NULL);

	//glEnable(GL_DEPTH_TEST);
	//glEnable(GL_BLEND);
	//glClearColor(1.0, 0.0, 0.0, 1.0);

	//glEnable(GL_LIGHTING);
	//glEnable(GL_LIGHT0);
	//GLfloat no_ambient[] = { 0.07, 0.07, 0.07, 1 }; // well, almost
	//glLightModelfv(GL_LIGHT_MODEL_AMBIENT, no_ambient);
	///glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 0);

	//glEnable(GL_FOG);
	//glFogf(GL_FOG_DENSITY, 0.08);

	//glEnable(GL_TEXTURE_2D);
	//glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

}

void CVideo::Draw()
{
	glLoadIdentity();
	glTranslatef(0.0, 0.0, -10.0);
	glColor3f(0.3f, 0.0f, 0.3f);
	glBegin(GL_QUADS);
	glVertex3f(-1.0f, -1.0f,  1.0f);
	glVertex3f( 1.0f, -1.0f,  1.0f);
	glVertex3f( 1.0f,  1.0f,  1.0f);
	glVertex3f(-1.0f,  1.0f,  1.0f);
	glVertex3f(-1.0f, -1.0f, -1.0f);
	glVertex3f(-1.0f,  1.0f, -1.0f);
	glVertex3f( 1.0f,  1.0f, -1.0f);
	glVertex3f( 1.0f, -1.0f, -1.0f);
	glVertex3f(-1.0f,  1.0f, -1.0f);
	glVertex3f(-1.0f,  1.0f,  1.0f);
	glVertex3f( 1.0f,  1.0f,  1.0f);
	glVertex3f( 1.0f,  1.0f, -1.0f);
	glVertex3f(-1.0f, -1.0f, -1.0f);
	glVertex3f( 1.0f, -1.0f, -1.0f);
	glVertex3f( 1.0f, -1.0f,  1.0f);
	glVertex3f(-1.0f, -1.0f,  1.0f);
	glVertex3f( 1.0f, -1.0f, -1.0f);
	glVertex3f( 1.0f,  1.0f, -1.0f);	
	glVertex3f( 1.0f,  1.0f,  1.0f);
	glVertex3f( 1.0f, -1.0f,  1.0f);
	glVertex3f(-1.0f, -1.0f, -1.0f);
	glVertex3f(-1.0f, -1.0f,  1.0f);
	glVertex3f(-1.0f,  1.0f,  1.0f);
	glEnd();

	glFlush();
	SDL_GL_SwapBuffers();	
}